package com.wit.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.wit.po.EmployeeRelation;

import com.wit.dao.EmployeeDao;
import com.wit.po.Employee;
import com.wit.po.Employeeaddition;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import hibernate.HibernateSessionFactory;

public class EmployeeDaoImpl implements EmployeeDao {

	
	public Employee getEmployeeById(String employeeId) {
		
		if(employeeId == null)
			return null;
		String hql = "from Employee e where e.employeeId=?";
		Session session = null;
		Employee employee = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			
			query.setString(0, employeeId);
			employee = (Employee)query.uniqueResult();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return employee;
	}

	
	public boolean modifyEmployeeInfo(Employee employee) {
		
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.update(employee);
			
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	public Map<String,String> getEmployeeNameList()
	{
		return s_GetEmployeeNameList();
	}
	
	static public Map<String,String> s_GetEmployeeNameList()
	{
		String hql = "select e.employeeId,e.employeeNameC from Employee e";
		Session session = null;
		HashMap<String,String> map = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
	
			List nameList = query.list();
			
			int size = nameList.size();
			map = new HashMap<String,String>();
			for(int i=0;i<size;i++)
			{
				map.put(((Object[])nameList.get(i))[0].toString(), ((Object[])nameList.get(i))[1].toString());
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return map;
	}
	
	public Employeeaddition getEmployeeAdditionById(String employeeId) {
		
		if(employeeId == null)
			return null;
		String hql = "from Employeeaddition a where a.employeeId=?";			//a.employee.employeeId=?
		Session session = null;
		Employeeaddition addition = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			
			query.setString(0, employeeId);
			addition = (Employeeaddition)query.uniqueResult();
			
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return addition;
	}

	
	public boolean modifyEmployeeAddition(Employeeaddition addition) {
		
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.update(addition);
			
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
	
	static public Map<String,String> s_GetPositionNameMap()
	{
		String hql = "select ep.employeePosId,ep.positionName from Employeeposition ep";
		Session session = null;
		HashMap<String,String> map = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
	
			List nameList = query.list();
			
			int size = nameList.size();
			map = new HashMap<String,String>();
			for(int i=0;i<size;i++)
			{
				map.put(((Object[])nameList.get(i))[0].toString(), ((Object[])nameList.get(i))[1].toString());
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return map;
	}
	
	static public Map<String,String> s_GetDepartmentNameMap()
	{
		String hql = "select d.departmentId,d.departNameC from Department d";
		Session session = null;
		HashMap<String,String> map = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
	
			List nameList = query.list();
			
			int size = nameList.size();
			map = new HashMap<String,String>();
			for(int i=0;i<size;i++)
			{
				map.put(((Object[])nameList.get(i))[0].toString(), ((Object[])nameList.get(i))[1].toString());
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return map;
	}

}
